using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;

namespace PHSRAG.Insight21.InsightWebControls
{
	/// <summary>
	/// Summary description for ClinicalTrialsSubNavigation.
	/// </summary>
	/// <summary>
	/// Summary description for ClinicalTrialsSubNavigation.
	/// </summary>
	[ToolboxData("<{0}:ClinicalTrialsSubNavigation runat=server />")]
	[Designer(typeof(ClinicalTrialsSubNavigationDesigner), typeof(IDesigner))]
	public class ClinicalTrialsSubNavigation : SubNavigation
	{
		#region Constants
		private const string	SelectedClinicalTrialsSubNavigationLinkKey = "SelectedClinicalTrialsubNavigationLink_3A31E802-4197-47fa-8D65-E771A4A40D77";
		#endregion

		#region Instance Variables
		private LinkButton budgetBuilderLinkButton;	
		#endregion

		#region Implementations for virtual methods from Base Class SubNavigation

		/// <summary>
		/// Create all controls in the ClinicalTrialsSubNavigation.
		/// </summary>
		protected override void CreateControls()
		{
			Controls.Clear();

			budgetBuilderLinkButton = new LinkButton();
			budgetBuilderLinkButton.CommandName = budgetBuilderLinkButton.Text = "Budget Builder";
			budgetBuilderLinkButton.CommandArgument = Utility.Settings.GetKeyValue("SubNavigation.ClinicalTrials.BudgetBuilderURL", "");
			
		}

		/// <summary>
		/// Setup the control tree to reflect the actual (and exact) layout of the controls within the ClinicalTrialsSubNavigation.
		/// </summary>
		protected override void GenerateControlTree()
		{
			Table table = new Table();
			Controls.Add(table);
			table.CssClass = "SubModuleSummaryNavigation";

			TableRow row = new TableRow();
			table.Rows.Add(row);

			TableCell cell = new TableCell();
			row.Cells.Add(cell);
			cell.CssClass = "SubModuleSummaryNavigationFillerCell";
			cell.Controls.Add(new LiteralControl("&nbsp;"));
			
			cell = new TableCell();
			row.Cells.Add(cell);

			cell = new TableCell();
			row.Cells.Add(cell);

			cell = new TableCell();
			row.Cells.Add(cell);

			table = new Table();
			cell.Controls.Add(table);

			row = new TableRow();
			table.Rows.Add(row);

			cell = new TableCell();
			row.Cells.Add(cell);
			cell.Controls.Add(budgetBuilderLinkButton);

			CreateSelectedLinkAdornments(row);
      }

		/// <summary>
		/// The selected link cell is given a prefix and suffix image as adornments. In addition the background of the
		/// cell is of a different style.
		/// </summary>
		private void CreateSelectedLinkAdornments(TableRow row)
		{
			LinkButton selectedLink = GetSelectedLink();
			if (selectedLink != null)
			{
				foreach (TableCell cell in row.Cells)
				{
					if ((cell.Controls.Count == 1) && (cell.Controls[0] == selectedLink))
					{
						cell.CssClass = "SubModuleNavigationSelectedSummary";

						TableCell adornmentCell = new TableCell();
						adornmentCell.CssClass = "SubModuleNavigationSelectedSummaryAdornment";
						row.Cells.AddAt(row.Cells.GetCellIndex(cell), adornmentCell);

						const string prefixImage = "../Images/Row2_Left.gif";
						System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(Context.Server.MapPath(prefixImage));
						Image image = new Image();
						image.ImageUrl = prefixImage;
						adornmentCell.Width = bitmap.Width;
						adornmentCell.Controls.Add(image);

						adornmentCell = new TableCell();
						adornmentCell.CssClass = "SubModuleNavigationSelectedSummaryAdornment";
						row.Cells.AddAt(row.Cells.GetCellIndex(cell) + 1, adornmentCell);

						const string suffixImage = "../Images/Row2_Right.gif";
						bitmap = new System.Drawing.Bitmap(Context.Server.MapPath(suffixImage));
						image = new System.Web.UI.WebControls.Image();
						image.ImageUrl = suffixImage;
						adornmentCell.Width = bitmap.Width;
						adornmentCell.Controls.Add(image);

						break;
					}
				}
			}
		}

		/// <summary>
		/// Retrieve the name of the link that was last clicked on.
		/// </summary>
		/// <returns>Reference to the link button corresponding to the clicked link</returns>
		protected override LinkButton GetSelectedLink()
		{
			// Anytime a link is clicked on, the OnBubbleEvent handler stores the name of the link in the session. Retrieve
			// that name and return the corresponding link button reference.

			object o = Page.Session[SelectedClinicalTrialsSubNavigationLinkKey];
			if (o != null)
			{
				string commandName = (string)o;
				if (commandName == budgetBuilderLinkButton.CommandName)
					return budgetBuilderLinkButton;
			 throw( new Exception( "Unsupported Command." ) );
			}
         return budgetBuilderLinkButton;
      }
		
		/// <summary>
		/// Gets the key used to the store the SelectedSubNavigationLink information
		/// in any hash table. 
		/// </summary>
		protected override string SelectedSubNavigationLinkKey
		{
			get 
			{
				return SelectedClinicalTrialsSubNavigationLinkKey;
			}
		}
				
		#endregion

		
		#region Event Handlers
		/// <summary>
		/// Disable view state.
		/// </summary>
		/// <param name="e">Argument associated with the load event.</param>
		protected override void OnLoad(EventArgs e)
		{
			EnableViewState = false;
			base.OnLoad (e);	
		}
		#endregion
	}

	#region Class ClinicalTrialsSubNavigationDesigner
	/// <summary>
	/// Designer associated with the control.
	/// </summary>
	public class ClinicalTrialsSubNavigationDesigner : ControlDesigner
	{
		#region Overrides
		/// <summary>
		/// Force the control to create its child controls for display at design time.
		/// </summary>
		/// <returns>HTML representation of the control</returns>
		public override string GetDesignTimeHtml()
		{
			((ClinicalTrialsSubNavigation)Component).Design();
			return base.GetDesignTimeHtml();
		}
		#endregion
	}
	#endregion
}
